<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use App\Pro;
use App\Att;
use App\Tou;

use DB;

require(base_path().'\vendor\alidayu\TopSdk.php');

class jieController extends Controller
{
    protected $middleware = ['App\Http\Middleware\Authenticate'=>[]];

    public function getJie(){
        return view('woyaojiekuan');
    }

    public function postJie(Request $req){
        $this->validate($req, [
          'age' => 'required|In:15,40,80',
          'money' => 'required|digits_between:2,7|regex:/^\d+$/',
          'mobile' => 'required|regex:/^1[34578]\d{9}$/',
          'imgcode' => 'required',
        ],
        [
          'required' => ':attribute 必须填写',
          'in' => ':attribute 必须是 :values 之一',
          'regex' => ':attribute 不合要求',
          'money.digits_between' => ':attribute 位数为2-7位',
        ]
        );

        $pro = new Pro();
        $att = new Att();
        
        $pro->name = $req->user()->name;
        $pro->uid = $req->user()->uid;
        $pro->money = $req->money;
        $pro->mobile = $req->mobile;
        $pro->pubtime = time();
        $pro->save();

        $att->pid = $pro->pid;
        $att->uid = $req->user()->uid;
        $att->age = $req->age;
        

        if($att->save()){
            return redirect('/');
        }

    }

    public function getpro(){
        $prs = DB::table('projects')->orderBy('pid','desc')->paginate(5);
        return view('prolist',['prs'=>$prs]);
    }

    public function check($pid){
        $p = pro::find($pid);
        if(empty($p)){
            return redirect('prolist');
        }
        return view('shenhe',['p'=>$p]);
    }

    public function postcheck($pid,Request $req){
        $p = pro::find($pid);
        $a = att::where('pid',$pid)->first();

        if(empty($p)){
            return redirect('prolist');
        }

        $p->hrange = $req->hrange;
        $p->rate = $req->rate;
        $p->status = $req->status;
        $p->title = $req->title;
        $p->pubtime = time();

        $a->title = $req->title;
        $a->realname = $req->realname;
        $a->udesc = $req->udesc;

        if($p->save() && $a->save()){
            return redirect('prolist');
        }
    }

    public function touzi($pid){
        $pro = pro::find($pid);
        return view('lijitouzi',['pro'=>$pro]);
    }

    public function posttz(Request $req,$pid){
        //判断平台回来的post是否合法
        $md5 = $req->v_amount.$req->v_moneytype.$req->v_oid.$req->v_mid.$req->v_url.'%()#QOKFDLS:1*&U';
        $md5 = strtoupper(MD5($mds));
        if($req->v_md5info !== $md5){
            return '证书错误！！';
        }
        
        $bid = new Tou();
        $bid->pid = $req->pid;
        $bid->uid = $req->user()->uid;
        $bid->title = $req->title;
        $bid->money = $req->money;
        $bid->pubtime = time();
        $bid->save();

        $p = pro::find($pid);
        if($p->money - $p->receive == $req->money){
            $this->tzdown($pid);
            $p->receive = $p->money;
        }else{
            $p->receive = $req->money;
        }
        
        $p->save();
     
        return redirect('/');       
    }

    public function tzdown($pid){
        $p = pro::find($pid);
        $p->status = 2;
        $p->save();

        //为借款者生成还款记录
        $amount = $p->money * $p->rate / 1200 + $p->money / $p->hrange;
        $row = ['pid'=>$p->pid,'uid'=>$p->uid,'title'=>$p->title];
        $row['amount'] = $amount;
        
        for($i=0;$i<$p->hrange;$i++){
            $row['paydate'] = date('Y-m-d',strtotime("+ $i month"));
            DB::table('hks')->insert($row);
        } 
        
        //为投资者生成收益记录
        $bids = Tou::where('pid',$pid)->get();
        $row = [];
        $row = ['pid'=>$pid,'title'=>$p->title];
        $row['enddate'] = date('Y-m-d',strtotime("+ $p->hrange month"));
        foreach($bids as $b){
          $row['uid'] = $b->uid;
          $row['amount'] = $b->money * $p->rate / 36500;
          DB::table('tasks')->insert($row);
        }
    }

    public function myzd($uid){
        $hks = DB::table('hks')->where('uid',$uid)->paginate(10);
        return view('myzd',['hks'=>$hks]);
    }

    public function mytz($uid){
        $bids = new Tou();
        $bid = $bids->where('uid',$uid)->paginate(10);
        return view('mytz',['bid'=>$bid]);
    }

    public function mysy($uid){
        $sy = DB::table('grows')->where('uid',$uid)->paginate(10);
        return view('mysy',['sy'=>$sy]);
    }

    public function pay(Request $req){
        $row = [];
        $row['v_amount'] = sprintf('%.2f',$req->money);
        $row['v_moneytype'] = 'CNY';
        $row['v_oid'] = date('Ymd').mt_rand(1000,9999); 
        $row['v_mid'] = '20272562';
        $row['v_url'] = 'ddd.com/touzi/'.$req->pid;
        $row['key'] = '%()#QOKFDLS:1*&U';
        $row['v_md5info'] = strtoupper(MD5(implode('',$row)));
        return view('pay',$row);
    }

    //alidayu短信验证
    public function sendsms(Request $req,$mobile){
        $rand = mt_rand(1000,9999);
        $req->session()->put('smcode',$rand);
        $name = $req->user()->name;
        $c = new \TopClient;
        $c->appkey = '23449741';
        $c->secretKey = '2af3acd6f599e7faea2d7a1dbbea46ff';
        $req = new \AlibabaAliqinFcSmsNumSendRequest;
        $req->setExtend("123456");
        $req->setSmsType("normal");
        $req->setSmsFreeSignName("测试");
        $req->setSmsParam("{\"name\":\"{$name}\",\"ms\":\"{$rand}\"}");
        $req->setRecNum($mobile);
        $req->setSmsTemplateCode("SMS_14300059");
        //$resp = $c->execute($req);
        var_dump($req);
    }

    public function checksm(Request $req,$code){
        if($req->session()->get('smcode') == $code){
          echo 1;
        }else{
            echo 0;
        }
    }
}
